﻿@page "/analyze"

<h4>Analyze</h4>
<div class="container-fluid bg-custom my-1 pb-1">
    @switch (pageState)
    {
        case PageState.Options:
            <div class="run-box bg-custom">
                <div class="form-inline">
                    <label class="mr-2" for="RunId">Run ID:</label>
                    <select class="form-control mr-3" id="RunId" @bind="RunIdInput">
                        <option value="-1"></option>
                        @for (int i = 0; i < Runs.Count; i++)
                        {
                            <option value="@i">@Runs[i]</option>
                        }
                    </select>
                    <label class="mr-2" for="SecondRunId">Second Run ID (Optional):</label>
                    <select class="form-control mr-2" id="SecondRunId" @bind="RunIdInput2">
                        <option value="-1"></option>
                        @for (int i = 0; i < Runs.Count; i++)
                        {
                            <option value="@i">@Runs[i]</option>
                        }
                    </select>
                    <button class="btn btn-primary" @onclick="BeginAnalyze">Start Analysis Scan</button>
                </div>
            </div>
            <AnalyzeOptionsRazor />
            break;
        case PageState.Analyzing:
            <Analyzing />
            break;
        case PageState.Finished:
            <button class="btn btn-primary" @onclick="GoToOptions">Analyze Again.</button>
            break;
        case PageState.Error:
            <p>An Error Occured while Analyzing.</p>
            break;
    }
</div>

@code{
    enum PageState
    {
        Options,
        Analyzing,
        Finished,
        Error
    }

    List<string> Runs = AttackSurfaceAnalyzerClient.DatabaseManager?.GetRuns() ?? new List<string>();

    int RunIdInput {
        get
        {
            return Runs.IndexOf(appData.ExportCollectCommandOptions.FirstRunId ?? string.Empty);
        }
        set
        {
            if (value >= 0)
            {
                appData.ExportCollectCommandOptions.FirstRunId = Runs[value];
            }
            else
            {
                appData.ExportCollectCommandOptions.FirstRunId = string.Empty;
            }
        }
    }
    int RunIdInput2
    {
        get
        {
            return Runs.IndexOf(appData.ExportCollectCommandOptions.SecondRunId ?? string.Empty);
        }
        set
        {
            if (value >= 0)
            {
                appData.ExportCollectCommandOptions.SecondRunId = Runs[value];
            }
            else
            {
                appData.ExportCollectCommandOptions.SecondRunId = string.Empty;
            }
        }
    }

    PageState pageState = PageState.Options;

    void GoToOptions()
    {
        pageState = PageState.Options;
    }

    async void BeginAnalyze()
    {
        await AnalyzeIt();
        pageState = PageState.Finished;
    }
    System.Threading.Timer? timer;

    async Task<ASA_ERROR> AnalyzeIt()
    {
        timer = new System.Threading.Timer((_) => InvokeAsync(() => StateHasChanged()), null, 0, 100);
        var firstRunId = RunIdInput != -1 ? Runs[RunIdInput] : string.Empty;
        var secondRunId = RunIdInput2 != -1 ? Runs[RunIdInput2] : string.Empty;

        appData.ExportCollectCommandOptions.FirstRunId = string.IsNullOrEmpty(secondRunId) ? secondRunId : firstRunId;
        appData.ExportCollectCommandOptions.SecondRunId = string.IsNullOrEmpty(secondRunId) ? firstRunId : secondRunId;
        pageState = PageState.Analyzing;

        await Task.Factory.StartNew(() =>
        {
            var CompareOneOptions = appData.CompareCommandOptions;
            CompareOneOptions.FirstRunId = appData.ExportCollectCommandOptions.FirstRunId;
            CompareOneOptions.SecondRunId = appData.ExportCollectCommandOptions.SecondRunId;
            var results = AttackSurfaceAnalyzerClient.CompareRuns(CompareOneOptions);
            AttackSurfaceAnalyzerClient.InsertCompareResults(results, CompareOneOptions.FirstRunId, CompareOneOptions.SecondRunId, appData.CompareCommandOptions.AnalysesFile?.GetHash() ?? string.Empty);
        });

        return ASA_ERROR.NONE;
    }

}
